package main

import (
	"fmt"
	"net/http"
	"os"

	config "./config"
	hdl "./handlers"
	captcha "./lib/captcha"
	rpc "./lib/jsonrpc2"
	log "./lib/log"
	mod "./models"
	"github.com/go-redis/redis"
)

const version = "1.0.0"

func main() {

	if os.Args[1] == "--version" ||
		os.Args[1] == "-v" {
		fmt.Println("Webapi Version: ", version)
		return
	}

	// 读取配置文件
	configFile := os.Args[1]
	log.Info("Reading config file at:", configFile)
	config.Load(configFile)

	// 启动数据库
	mod.Init(config.PostgresHost)

	// 启动redis
	redisdb := redis.NewClient(&redis.Options{
		Addr: ":6379",
	})
	// captcha模块需要注册redis：
	if e := captcha.Init(redisdb); e != nil {
		log.Fatal(e)
	}
	log.Success("Connected to Redis on 6379")

	// 这里注册所有处理方法
	rpc.RegisterFunc("UserGetCaptcha", hdl.UserGetCaptcha)
	rpc.RegisterFunc("UserGetSignupMobileCode", hdl.UserGetSignupMobileCode)
	rpc.RegisterFunc("UserSignupByMobileCodePass", hdl.UserSignupByMobileCodePass)
	rpc.RegisterFunc("UserSigninByMobilePass", hdl.UserSigninByMobilePass)
	rpc.RegisterFunc("UserUpdateBasicInfo", hdl.UserUpdateBasicInfo)
	rpc.RegisterFunc("UserGetBasicInfo", hdl.UserGetBasicInfo)

	log.Success("RPC server listening on 10000")
	http.HandleFunc("/", rpc.RPCHandler)

	// 启动文件上传服务
	hdl.UploadFileInit()

	log.Fatal(http.ListenAndServe(":10000", nil))
}
